package com.ushio.capsuleservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.capsuleservice.mapper.PostMapper;
import com.ushio.capsuleservice.model.entity.Post;
import com.ushio.capsuleservice.service.PostService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 帖子服务实现类
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    
    @Override
    public Post createPost(Post post) {
        post.setStatus(1);
        post.setViewCount(0);
        post.setLikeCount(0);
        post.setReplyCount(0);
        post.setCreatedTime(LocalDateTime.now());
        post.setUpdatedTime(LocalDateTime.now());
        
        save(post);
        return post;
    }
    
    @Override
    public IPage<Post> getPostList(int page, int size, String keyword) {
        Page<Post> pageParam = new Page<>(page, size);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq("status", 1);
        if (keyword != null && !keyword.trim().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like("title", keyword)
                .or()
                .like("content", keyword)
                .or()
                .like("tags", keyword)
            );
        }
        queryWrapper.orderByDesc("created_time");
        
        return page(pageParam, queryWrapper);
    }
    
    @Override
    public Post getPostDetail(Long postId) {
        Post post = getById(postId);
        if (post != null && post.getStatus() == 1) {
            // 增加浏览次数
            incrementViewCount(postId);
            return post;
        }
        return null;
    }
    
    @Override
    public Post updatePost(Long postId, Post post) {
        Post existingPost = getById(postId);
        if (existingPost == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        existingPost.setTitle(post.getTitle());
        existingPost.setContent(post.getContent());
        existingPost.setTags(post.getTags());
        existingPost.setUpdatedTime(LocalDateTime.now());
        
        updateById(existingPost);
        return existingPost;
    }
    
    @Override
    public boolean deletePost(Long postId) {
        Post post = getById(postId);
        if (post == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        post.setStatus(0);
        post.setUpdatedTime(LocalDateTime.now());
        
        return updateById(post);
    }
    
    @Override
    public void incrementViewCount(Long postId) {
        Post post = getById(postId);
        if (post != null) {
            post.setViewCount(post.getViewCount() + 1);
            updateById(post);
        }
    }
}
